// import _ from 'ladash'
import {processAppConfig} from "@/util/appConfigUtil";
import moment from "moment";


//1 分组
//2 处理变量 映射关系
//3 生成导出列
//处理考场安排

const appConfig = processAppConfig()

//处理考场安排
export function processArrange(rows, globalMap = new Map()) {

    let tempRows = JSON.parse(JSON.stringify(rows))
    let exportColumn = appConfig.exportConfig.exportColumn
    let exportRows = []
    for (let i = 0; i < tempRows.length; i++) {
        let row = tempRows[i];
        //处理每一行
        processRow(row, exportColumn, globalMap)
        let rowObj = {}
        exportColumn.forEach(key => {
            return rowObj[key] = (row[key] || globalMap.get(key))
        })
        exportRows.push(rowObj)
    }

    return exportRows;
}

//执行函数并打印函数执行时间
export function logTimeSpan(func) {
    let startTime = new Date()
    func()
    let endTime = new Date()
    console.log(`执行函数[${func.name}]耗时  ：` + (endTime - startTime) / 1000 + 's')
}

//处理每一行
export function processRow(row, column, globalMap = new Map()) {
    const variableMap = new Map()
    let keys = Object.keys(row)
    for (let i = 0; i < keys.length; i++) {
        let rowKey = keys[i]
        if (row[rowKey]) {
            variableMap.set(rowKey, row[rowKey])
        }
    }

    for (let key of globalMap.keys()) {
        variableMap.set(key, globalMap.get(key))
    }

    //加载通用变量
    loadCustomVariableMap(variableMap)

    for (let rowKey of column) {

        let item = appConfig.columnDefines[rowKey]
        if (item && item.type === 'template') {
            //填充模板变量
            row[item.prop] = replaceAllByMap(item.source, variableMap);
        } else if (item && item.type === 'select') {
            //映射字典
            row[rowKey] = item.dicMap.get(row[rowKey])
        } else if (item && item.type === 'datetime') {
            //格式化时间

            var time = variableMap.get(rowKey);
            if (time instanceof Object) {

                row[rowKey] = moment(time).format(item.pattern)
            } else {
                row[rowKey] = time
            }
        }


        if (row[rowKey] === null || row[rowKey] === undefined) {
            row[rowKey] = variableMap.get(rowKey)
        }
    }
}

//替换变量
export function replaceAllByMap(pattern, map) {
    let RegExp = /\{\w+}/g
    return pattern.replace(RegExp, function (match) {
        return map.get(match.slice(1, match.length - 1))
    })
}

//加载通用变量
export function loadCustomVariableMap(variableMap) {
    variableMap.set('now', new Date())
    variableMap.set('nowStr', moment().format('YYYY-MM-DD HH:mm'));


}


//处理每一批的数据 subject->campus->batchIndex->examHallKey
export function processBatchItemData(arrangeData, subject, campus, batchIndex, batchTimeData) {
    console.log(subject, campus, batchIndex, batchTimeData)

    var batchItemArrangeData = arrangeData[subject][campus].batch[batchIndex];

    let processArrangeData = []
    for (let examHallKey in batchItemArrangeData) {

        let batchTimeItem = batchTimeData.find(timeItem => timeItem.examHallKey === examHallKey)
        if (!batchTimeItem) continue;
        console.log('batchTimeItem', batchTimeItem)
        let batchItemArrangeDatum = batchItemArrangeData[examHallKey];
        let map = new Map();

        //加入到变量
        for (let examHallDataKey in batchTimeItem) {
            map.set(examHallDataKey, batchTimeItem[examHallDataKey])
        }
        map.set("batchId", batchTimeItem.id)


        let campusName = appConfig.columnDefines.campus.dicMap.get(campus)
        map.set("campusName", campusName)
        //处理行
        processArrangeData = processArrangeData.concat(processArrange(batchItemArrangeDatum, map));

    }

    return processArrangeData;
}

